package org.NooLab.randomgraph.app;

import java.util.ArrayList;

import org.NooLab.randomgraph.RandomGraphFactory;
import org.NooLab.randomgraph.interfaces.RGraphApplicationEventIntf;
import org.NooLab.randomgraph.interfaces.RGraphProcessControlIntf;
import org.NooLab.randomgraph.interfaces.RGraphPropertiesIntf;
import org.NooLab.randomgraph.properties.RGraphProperties;
import org.NooLab.randomgraph.properties.RGraphPropertiesDefaults;
import org.NooLab.utilities.files.DFutils;



/**
 * 
 * in class-mode the sequence of calls would be the following:
 
 		// 1.defining instance and base folder, necessary for creating the db file and 
 		//   for externalizing the configuration file 
 		RandomGraphFactory.setStartupLocations( RandomGraphFactory._INSTANCE_TYPE_TEXT,
												"/data/iTexx/RGraph/"); //defaultLocationPath);

		// 2. calling an analog for prepareRGrapher(), through
		//    implementing "RGraphPrepareIntf", possibly using the default via rpi.prepareRGrapher();
		rpi = RGraphPrepare.getInstance();
		 
		
		// 3. starting the thread 
		public void startInstance();
		
		
		
 * 
 * @author kwa
 *
 */
public class RGraphPrepare 
							implements 	Runnable,
										RGraphPrepareIntf,
										// for messages from the engine to the outer application layers like this module
										RGraphApplicationEventIntf {

	public static final int _USE_MODE_TEST  = 99 ;
	public static final int _USE_MODE_CLASS = 0 ;
	
	RandomGraphFactory rgFactory;
	RGraphProperties rgProperties;
	RGraphPropertiesDefaults rgPropertiesDefaults;

	boolean batchMode = false;
	int appMode = 0;
	
	String sourceForProperties = "";

	Thread rgThrd;
	private String lastProjectName;
	private int instanceType;
	private RGraphProcessControlIntf rgProcessControl;

	String fileSource ="";
	private static  RGraphPrepare rgPrepare;
	
	// ------------------------------------------------------------------------
	public RGraphPrepare() {

		rgThrd = new Thread(this, "RgInstance");
		
	}
	
	public static RGraphPrepareIntf getInstance(){
		rgPrepare = new RGraphPrepare();
		
		return rgPrepare ;
	}
	
	// ------------------------------------------------------------------------
	
	// this is just for applet-mode == testing mode
	public static void first() throws Exception {
	/*
	 * this needs to be set by the master application using RGraph as a class lib	
	 */
		
		RandomGraphFactory.setStartupLocations( RandomGraphFactory._INSTANCE_TYPE_TEXT,
												"/data/iTexx/RGraph/"); //defaultLocationPath);
		

	}


	public void startInstance() {

		appMode = _USE_MODE_CLASS ;
		
		rgThrd.start();

	}

	public void startTestInstance() {
		appMode = _USE_MODE_TEST ;
		lastProjectName = IniProperties.lastProjectName;
		rgThrd.start();

	}

	
	private void startUp() {
	
		ArrayList<String> files = new ArrayList<String>();
	
		try {
			
			if (rgFactory==null){
				prepareRGrapher() ;
			}
			
			if (appMode == _USE_MODE_CLASS){
				rgFactory.start();
			}else{
				rgFactory.start(fileSource);
				
			}
	
		} catch (Exception e) {
			e.printStackTrace();
		}
	
	}

	// ------------------------------------------------------------------------

	@Override
	public void run() {

		startUp();
	}

	public void issueUserbreak() {
		//
		if (rgProcessControl != null) {
			rgProcessControl.interrupt(0);
		} else {
			System.err.println("Nothing to stop, <somProcessControl> was not up and running...");
		}
	}

	public void prepareRGrapher() {

		// in app mode, we have to set the basepath from external

		rgProperties = new RGraphProperties();
		rgPropertiesDefaults = rgProperties.getRgPropertiesDefaults() ;
		
		
		rgProperties.setInitializationOK(true);
		
		rgProperties.setSystemRootDir( IniProperties.fluidSomProjectBasePath) ;
		rgProperties.setProjectName( IniProperties.lastProjectName );
		
		rgProperties.initializeDefaults();
		
		
		try {
			rgFactory = RandomGraphFactory.get(rgProperties);

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if (rgFactory==null){
			System.exit(-3);
		}
		rgFactory.saveStartupTrace( RandomGraphFactory._INSTANCE_TYPE_TEXT,
									rgPropertiesDefaults.getStartupTraceInfo());

		rgProperties.save();

		// the project is equal to the instance: no user-based projects!

	}

	// only for testing
	public void defineTestFile( String filename){

		ArrayList<String> files = new ArrayList<String>() ;
		
		try {

			fileSource = filename ;
			RGraphStartup.getLastDataSet();

			
			if (DFutils.fileExists(fileSource) == false) {
				String dir = DFutils.createPath(IniProperties.fluidSomProjectBasePath,
						RGraphStartup.getLastProjectName() + "/data/raw/");
				files = DFutils.listOfFiles("", "", dir);
				if (files.size() > 0) {
					fileSource = files.get(0);
				} else {
					throw (new Exception("No text file found."));
				}
			} else {
				files.add(fileSource);
			}


		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public RGraphProperties getProperties() {
		return rgProperties;
	}

}
